<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Health_mdl extends MY_Model {

	const DB_HEALTH = 'health';		//食材数据库
	const DB_SHICAI = 'shicai_new';		//食材分类数据库

    public function __construct()
    {
		parent::__construct();
		$this->table = 'health';
    }
    //获取文章内容
    public function get_health_by_id($id)
    {
    	$this->load->library('category');
    	$data = array();
		$this->db->select('*')->from(self::DB_HEALTH)->where('id', intval($id))->limit(1);
		$query = $this->db->get();
		if($query->num_rows() == 1)
		{
			$data = $query->row_array();
			$cate = $this->category->fetchOne($data['cid']);
			@$data['cateName'] = $cate['cateName'];
		}
		$query->free_result();
		
		return $data;
    }

    //获取文章列表classid
    public function get_health_list($cid = NULL,$offset = NULL, $limit = NULL, $orderby = 'id DESC')
    {
		$this->db->select('*');
		if($cid && is_numeric($cid))
		{
			$this->db->where('cid', intval($cid));
		}
		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}
		$this->db->order_by($orderby); 
        return $this->db->get(self::DB_HEALTH);
    }


    //获取食材列表
    public function get_health_by_shicai_id($sid)
    {
		$this->db->select('id,title,pic');
		$this->db->where_in('title', explode(',',$sid));
        return $this->db->get(self::DB_SHICAI);
    }

    //搜索
    public function get_health_search($keywords, $offset = NULL, $limit = NULL, $orderby = 'id DESC')
    {
		$this->db->select('*');
		$this->db->like('title', $this->db->escape_like_str($keywords));

		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}

		$this->db->order_by($orderby); 

        return $this->db->get(self::DB_HEALTH);
    }

    public function update_views($id)
    {
		$this->db->set('views','views+1',FALSE);
		$this->db->where('id',$id);
    	return $this->db->update(self::DB_HEALTH);
    }

	//***********************后台专用*********************************************************************
	public function health_insert($arr)
	{
		return $this->db->insert(self::DB_HEALTH,$arr);
	}

	public function health_update($arr, $id)
	{
		return $this->db->update(self::DB_HEALTH, $arr, 'id = '.$id);
	}

	public function health_delete($id)
	{
		$this->db->select('pic,content')->where('id', intval($id))->limit(1);
		$query = $this->db->get(self::DB_HEALTH)->row_array();
		//删除缩略图
		if ($query['pic'])
		{
			unlink('.'.$query['pic']);
		}
		//删除内容中 图片

		$pattern="/<img.+?src=[\\\\]?\"(.+?)[\\\\]?\"/i";
		preg_match_all($pattern,$query['content'], $img_array, PREG_PATTERN_ORDER);
		$img_array = array_unique($img_array[1]);
		if ($img_array)
		{
			foreach ($img_array as $img) 
			{
				unlink('.'.$img);
			}
		}
		//删除内容
		return $this->db->where('id',$id)->delete(self::DB_HEALTH);
	}
}

/* End of file shicai_model.php */
/* Location: ./application/models/shicai_model.php */